import axios from 'axios'

const state = {
  users: [],
  stats: {
    totalUsers: 0,
    onlineUsers: 0,
    newUsers: 0
  },
  roleStats: {}
}

const mutations = {
  SET_USERS(state, users) {
    state.users = users
  },
  SET_STATS(state, stats) {
    state.stats = stats
  },
  SET_ROLE_STATS(state, roleStats) {
    state.roleStats = roleStats
  },
  ADD_USER(state, user) {
    state.users.push(user)
  },
  UPDATE_USER(state, updatedUser) {
    const index = state.users.findIndex(u => u.id === updatedUser.id)
    if (index !== -1) {
      state.users.splice(index, 1, updatedUser)
    }
  },
  REMOVE_USER(state, userId) {
    state.users = state.users.filter(u => u.id !== userId)
  }
}

const actions = {
  async fetchUsers({ commit }) {
    try {
      const response = await axios.get('/api/users')
      commit('SET_USERS', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async fetchUserStats({ commit }) {
    try {
      const response = await axios.get('/api/users/stats')
      commit('SET_STATS', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async fetchUserRoleStats({ commit }) {
    try {
      const response = await axios.get('/api/users/role-stats')
      commit('SET_ROLE_STATS', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async fetchUserGrowthStats({ commit }) {
    try {
      const response = await axios.get('/api/users/growth-stats')
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async createUser({ commit }, userData) {
    try {
      const response = await axios.post('/api/users', userData)
      commit('ADD_USER', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async updateUser({ commit }, { id, userData }) {
    try {
      const response = await axios.put(`/api/users/${id}`, userData)
      commit('UPDATE_USER', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async deleteUser({ commit }, id) {
    try {
      await axios.delete(`/api/users/${id}`)
      commit('REMOVE_USER', id)
      return true
    } catch (error) {
      throw error
    }
  },
  
  async updateUserPassword({ commit }, { id, newPassword }) {
    try {
      const response = await axios.put(`/api/users/${id}/password`, { newPassword })
      commit('UPDATE_USER', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async updateUserRole({ commit }, { id, role }) {
    try {
      const response = await axios.put(`/api/users/${id}/role`, { role })
      commit('UPDATE_USER', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async fetchUsersByTimeRange({ commit }, { start, end }) {
    try {
      const response = await axios.get(`/api/users/time-range?start=${start}&end=${end}`)
      commit('SET_USERS', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async fetchUserCount({ commit }) {
    try {
      const response = await axios.get('/api/users/count')
      return response.data
    } catch (error) {
      throw error
    }
  },

  
  async fetchNewUsers({ commit }) {
    try {
      const response = await axios.get('/api/users/new')
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async fetchOnlineUsers({ commit }) {
    try {
      const response = await axios.get('/api/users/online')
      return response.data
    } catch (error) {
      throw error
    }
  }
}

const getters = {
  allUsers: state => state.users
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}